package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmSensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.IWmSensitiveService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements IWmSensitiveService {
    /**
     * 新增敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {
        //校验请求参数是否存在
        if (StringUtils.isBlank(wmSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //参数有值,则根据敏感词去数据库查询,看是否能查询出相同的敏感词
        WmSensitive one = this.getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if (one != null){
            //能查询出来,说明敏感词已经存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //调用方法,存储敏感词
        wmSensitive.setCreatedTime(new Date());
        this.save(wmSensitive);
        return ResponseResult.okResult(200,"操作成功");
    }

    /**
     * 查询敏感词列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listSensitive(WmSensitiveDto dto) {
        //校验请求参数
        if (dto.getPage() == null || dto.getSize() == null){
            //提示缺少参数
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //构造分页查询条件
        Page<WmSensitive> page = new Page<>(dto.getPage(),dto.getSize());
        //判断请求参数中有无查询条件
        QueryWrapper<WmSensitive> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())){
            //构造条件查询条件
            queryWrapper.like("sensitives",dto.getName());
        }
        //按照创建时间倒序查询
        queryWrapper.orderByDesc("created_time");
        //调用方法执行查询操作
        Page<WmSensitive> pageInfo = this.page(page, queryWrapper);
        //封装结果集,创建分页结果返回对象
        PageResponseResult responseResult = new PageResponseResult();
        responseResult.setData(pageInfo.getRecords());
        responseResult.setCurrentPage(dto.getPage());
        responseResult.setSize(dto.getSize());
        responseResult.setTotal((int) pageInfo.getTotal());

        return responseResult;
    }

    /**
     * 修改敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        //校验请求参数是否存在
        if (wmSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //请求参数存在则调用方法修改数据库中的数据即可
        this.updateById(wmSensitive);

        return ResponseResult.okResult(wmSensitive);
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        //校验参数是否存在
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //直接根据id删除即可
        this.removeById(id);

        return ResponseResult.okResult(200,"操作成功");
    }
}
